<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('基金名称')" prop="fundName">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/fund/fundBasehome',query:{id:form.fundId}}"
                class="link_a"
              >{{ form.fundName }} </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金管理人')" prop="fundManager">

            <el-select v-model="form.fundManager" filterable clearable :placeholder="$t('请选择')" disabled>
              <el-option
                v-for="item in gpBaseOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                disabled
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('会议时间')" prop="meetingTime">
            <el-date-picker
              v-model="form.meetingTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('会议时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('会议地点')" prop="meetingPlace">
            <el-input v-model="form.meetingPlace" :placeholder="$t('会议地点')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('会议类型')" prop="meetingType">
            <dict-select
              v-model="form.meetingType"
              dictionary="partner_meeting_type"
              :placeholder="$t('会议类型')"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item v-if="form.meetingType === 'FIXED'" :label="$t('会议周期')" prop="meetingCycle">
            <dict-select
              v-model="form.meetingCycle"
              dictionary="partner_meeting_cycle"
              :placeholder="$t('会议周期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('会议成员')" prop="meetingMembers">
            <el-input v-model="form.meetingMembers" type="textarea" :placeholder="$t('会议成员')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('会议议题')" prop="meetingAgenda">
            <el-input v-model="form.meetingAgenda" type="textarea" :placeholder="$t('会议议题')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="fund_partner_meeting" :business-id="form.fundId" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="fund:partner" />
    </div>
  </div>
</template>

<script>
    import { saveFundPartnerMeeting, getFundPartnerMeeting } from '@/api/fund/partner/fundPartnerMeeting'
    import { getFundBase } from '@/api/fund/fundBase'
    import { findGp } from '@/api/fund/gp/gpBase'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    fundManager: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    meetingTime: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    meetingPlace: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    meetingType: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    meetingCycle: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    meetingMembers: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    meetingAgenda: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ]
                },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true,
                // 是否隐藏保存按钮
                otherBtnHide: false,
                // 基金管理人
                gpBaseOptions: [],
                // 使用流程的Key
                processKey: undefined
            }
        },
        created() {
            this.getDicts('partner_meeting_type').then(response => {
                this.meetingTypeOptions = response.data
            })
            this.getDicts('partner_meeting_cycle').then(response => {
                this.meetingCycleOptions = response.data
            })
            this.$bus.$emit('async_done')
            // 查询基金管理人
            this.findGpOption()
        },
        methods: {
            // 初始化数据
            init(data, processKey) {
                // 使用流程
                this.processKey = processKey
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getFundPartnerMeeting(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                        this.loading = false
                    })
                } else {
                    this.reset()
                    // 获取基金详细
                    getFundBase(this.form.fundId).then(response => {
                      this.form.fundName = response.data.fundFullName
                      this.form.fundManager = response.data.fundBaseFund.managerId
                      this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    fundId: this.$route.query.id,
                    fundName: undefined,
                    fundManager: undefined,
                    meetingTime: undefined,
                    meetingPlace: undefined,
                    meetingType: undefined,
                    meetingCycle: undefined,
                    meetingMembers: undefined,
                    meetingAgenda: undefined,
                    remark: undefined,
                    actTitle: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                    this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm(type) {
                return new Promise((resolve, reject) => {
                  this.$refs['form'].validate(valid => {
                    // 验证数据
                    if (valid) {
                      // 弹出遮罩层
                      this.loading = true
                      // 保存数据
                      saveFundPartnerMeeting(this.form).then(response => {
                        if (response.code === 200) {
                          // 点击的按钮是保存并启动流程
                          if (type === 'process') {
                            // 启动流程
                            this.startProcess(response.data.id)
                            // 点击的仅保存
                          } else {
                            this.msgSuccess(this.$t('保存成功'))
                            // 关闭form页面
                            this.closeForm()
                          }
                          resolve()
                        } else {
                          this.msgError(response.msg)
                          this.loading = false
                          reject()
                        }
                      })
                    } else {
                      reject()
                      this.locationError(document)
                    }
                  })
                }).catch(() => {
                    this.loading = false
                })
           },
            // 启动流程
           startProcess(dataId) {
               this.loading = false
               // 启动流程时候传入的参数
               const processData = {}
               // 待办标题
               processData.title = this.form.fundName + '-合伙人会议'
               // 业务表名
               processData.dataTable = 'fund_partner_meeting'
               // 业务ID
               processData.dataId = dataId
               // 业务表单地址(待办页面需要打开的页面)
               processData.formPath = '@/views/fund/partner/fundPartnerMeetingForm'
               // 使用流程的Key
               processData.processKey = this.processKey
               // 菜单标识(根据菜单标识判断的走哪个流程)
               // processData.menuKey = this.$t('菜单标识')
               this.startProcessByMenu(processData, () => {
                   this.form.actStatus = 'D'
                   this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
                   this.closeForm()
               })
           },
           // 查询基金管理人
           findGpOption() {
             findGp().then(response => {
               this.gpBaseOptions = response.data
             })
           }
        }

    }
</script>
